#!/usr/bin/env python

import wx
import wx.lib.masked
import __measure as measure

from _frame import Frame

class Measure(Frame):
    
    def __init__(self, parent=None):
        super(Measure, self).__init__(parent, name='Frame')
        
        self.ToolBar = measure.ToolBar(self)
        self.ToolBar.Realize()
        self.StatusBar = wx.StatusBar(self)
        
        self.realize()
        
    def realize(self):
        panel = wx.Panel(self)
        
        gridSizer = wx.FlexGridSizer(cols=2, vgap=5, hgap=5)
        gridSizer.AddGrowableCol(1)
        
        self.locationText = wx.StaticText(panel, label='Location:')
        gridSizer.Add(self.locationText, 0, wx.ALIGN_CENTER_VERTICAL)
        
        self.location = wx.TextCtrl(panel)
        gridSizer.Add(self.location, 1, wx.EXPAND)
        
        self.saveToText = wx.StaticText(panel, label='Save to:')
        gridSizer.Add(self.saveToText, 0, wx.ALIGN_CENTER_VERTICAL)
        
        self.saveTo = measure.SaveTo(panel, label='')
        gridSizer.Add(self.saveTo, 1, wx.EXPAND)
        
        self.lengthText = wx.StaticText(panel, label='Record Length:')
        gridSizer.Add(self.lengthText, 0, wx.ALIGN_CENTER_VERTICAL)
        
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        
        self.length = wx.lib.masked.NumCtrl(panel, integerWidth=2,
                                            fractionWidth=1)
        self.length.Font = self.location.Font
        sizer.Add(self.length, 1, wx.EXPAND)
        sizer.AddSpacer(5)
        
        self.lengthSecondText = wx.StaticText(panel, label='seconds')
        sizer.Add(self.lengthSecondText, 0, wx.ALIGN_CENTER_VERTICAL)
        
        gridSizer.Add(sizer, 1, wx.EXPAND)
        
        self.triggerText = wx.StaticText(panel, label='Trigger:')
        gridSizer.Add(self.triggerText, 0, wx.ALIGN_CENTER_VERTICAL)
        
        choices = ['Trigger 1', 'Trigger 2']
        style = wx.CB_DROPDOWN | wx.CB_READONLY
        self.trigger = wx.ComboBox(panel, choices=choices, style=style)
        gridSizer.Add(self.trigger, 1, wx.EXPAND)
        
        self.sensitivityText = wx.StaticText(panel,
                                             label='Trigger Sensitivity:')
        gridSizer.Add(self.sensitivityText, 0, wx.ALIGN_CENTER_VERTICAL)
        
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.sensitivityLowText = wx.StaticText(panel, label='L')
        sizer.Add(self.sensitivityLowText, 0, wx.ALIGN_CENTER_VERTICAL)
        
        self.sensitivity = wx.Slider(panel, minValue=1, maxValue=10)
        sizer.Add(self.sensitivity, 1, wx.EXPAND)
        
        self.sensitivityHighText = wx.StaticText(panel, label='H')
        sizer.Add(self.sensitivityHighText, 0, wx.ALIGN_CENTER_VERTICAL)
        
        gridSizer.Add(sizer, 1, wx.EXPAND)
        
        self.stackText = wx.StaticText(panel, label='Stack Count:')
        gridSizer.Add(self.stackText, 0, wx.ALIGN_CENTER_VERTICAL)
        
        self.stack = wx.SpinCtrl(panel, min=1, initial=1)
        gridSizer.Add(self.stack, 1, wx.EXPAND)
        
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.AddSpacer(3)
        sizer.Add(gridSizer, 1, wx.EXPAND | wx.LEFT | wx.RIGHT, 15)
        sizer.AddSpacer(5)
        panel.SetSizerAndFit(sizer)
        
        # Set best size
        self.SetInitialSize()
        self.MinSize = self.Size
        # Lock height
        self.MaxSize = (-1, self.Size[1])
        
    def start(self):
        tool = self.ToolBar
        tool.start.disable()
        tool.stop.enable()
        tool.setting.disable()
        
        self.locationText.Disable()
        self.location.Disable()
        self.saveToText.Disable()
        self.saveTo.Disable()
        self.lengthText.Disable()
        self.length.Disable()
        self.lengthSecondText.Disable()
        self.triggerText.Disable()
        self.trigger.Disable()
        self.sensitivityText.Disable()
        self.sensitivityLowText.Disable()
        self.sensitivity.Disable()
        self.sensitivityHighText.Disable()
        self.stackText.Disable()
        self.stack.Disable()
    
    def stop(self):
        tool = self.ToolBar
        tool.start.enable()
        tool.stop.disable()
        tool.setting.enable()
        
        self.locationText.Enable()
        self.location.Enable()
        self.saveToText.Enable()
        self.saveTo.Enable()
        self.lengthText.Enable()
        self.length.Enable()
        self.lengthSecondText.Enable()
        self.triggerText.Enable()
        self.trigger.Enable()
        self.sensitivityText.Enable()
        self.sensitivityLowText.Enable()
        self.sensitivity.Enable()
        self.sensitivityHighText.Enable()
        self.stackText.Enable()
        self.stack.Enable()
        
    def getStatus(self):
        return self.StatusBar.StatusText
    
    def setStatus(self, value):
        self.StatusBar.SetStatusText('%s' % value)
    
    status = property(getStatus, setStatus)